﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Trade.Controls;
using Trade.DAL;
using Trade.Model;

namespace Trade
{
    public partial class FrmPurchaseMain : Trade.Controls.RayForm
    {
        public bool condApply = false;
        public string condPurchaseNo;
        public string condReferenceNo;
        public string condVendor;
        public string condWarehouse;
        public string condBuyer;
        public string condEntryDate;
        public bool condStatus;

        private Purchase purchase = new Purchase();
        public DataTable dtMaster = null;
        private BindingSource bs;
        public int curRowIndex = -1;

        public FrmPurchaseMain()
        {
            InitializeComponent();
        }

        //新增
        private void tsbtnCreate_Click(object sender, EventArgs e)
        {
            FrmPurchase purchaseForm = new FrmPurchase();
            purchaseForm.Tag = Utility.CRUD.Insert;
            purchaseForm.Owner = this;
            purchaseForm.ShowDialog();
        }

        //查询
        private void tsbtnRetrieve_Click(object sender, EventArgs e)
        {
            FrmPurchaseSearchCondition purchaseCondForm = new FrmPurchaseSearchCondition();
            purchaseCondForm.Owner = this;
            purchaseCondForm.ShowDialog();

            int warehouse = (condWarehouse == "" ? 0 : Convert.ToInt16(condWarehouse));
            DateTime entryDate = (condEntryDate == "" ? DateTime.Today : Convert.ToDateTime(condEntryDate));

            if (condApply == true)
            {
                dtMaster = purchase.GetPurchaseMaster(condPurchaseNo, condReferenceNo, condVendor, warehouse, condBuyer, entryDate, condStatus);
                bs = new BindingSource(this.dtMaster, null);
                dgvPurchaseOrder.DataSource = bs;
                SetMasterDataGridView();
                dgvPurchaseOrder.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                if (dgvPurchaseOrder.RowCount > 0)
                {
                    dgvPurchaseOrder.Select();
                    tsbtnUpdate.Enabled = true;
                    tsbtnDelete.Enabled = true;
                }
                else
                {
                    tsbtnUpdate.Enabled = false;
                    tsbtnDelete.Enabled = false;
                }
            }
        }

        //修改
        private void tsbtnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvPurchaseOrder.SelectedCells != null)
            {
                string curPurchaseNo = dgvPurchaseOrder.Rows[dgvPurchaseOrder.CurrentCell.RowIndex].Cells["PurchaseNo"].Value.ToString();

                if (string.IsNullOrEmpty(curPurchaseNo) == false)
                {
                    FrmPurchase purchaseForm = new FrmPurchase();
                    curRowIndex = dgvPurchaseOrder.CurrentCell.RowIndex;    //取得当前RowIndex
                    purchaseForm.Owner = this;
                    purchaseForm.Tag = Utility.CRUD.Update;
                    purchaseForm.ShowDialog();
                                       
                }
            }
        }

        //删除
        private void tsbtnDelete_Click(object sender, EventArgs e)
        {
            if (dgvPurchaseOrder.SelectedCells != null)
            {
                string curPurchaseNo = dgvPurchaseOrder.Rows[dgvPurchaseOrder.CurrentCell.RowIndex].Cells["PurchaseNo"].Value.ToString();

                if (string.IsNullOrEmpty(curPurchaseNo) == false)
                {
                    FrmPurchase purchaseForm = new FrmPurchase();
                    curRowIndex = dgvPurchaseOrder.CurrentCell.RowIndex;
                    purchaseForm.Owner = this;
                    purchaseForm.Tag = Utility.CRUD.Delete;
                    purchaseForm.ShowDialog();
                }
            }
        }

        //退出
        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SetMasterDataGridView()
        {
            dgvPurchaseOrder.Columns[0].Width = 100;
            dgvPurchaseOrder.Columns[0].Name = dtMaster.Columns["PurchaseNo"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[0].HeaderText = "进货单号";
            dgvPurchaseOrder.Columns[1].Width = 80;
            dgvPurchaseOrder.Columns[1].Name = dtMaster.Columns["VendorNo"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[1].HeaderText = "供货单位";
            dgvPurchaseOrder.Columns[2].Width = 80;
            dgvPurchaseOrder.Columns[2].Name = dtMaster.Columns["Dept"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[2].HeaderText = "部门编号";
            dgvPurchaseOrder.Columns[3].Width = 150;
            dgvPurchaseOrder.Columns[3].Name = dtMaster.Columns["ContractName"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[3].HeaderText = "合同名称";
            dgvPurchaseOrder.Columns[4].Width = 100;
            dgvPurchaseOrder.Columns[4].Name = dtMaster.Columns["ContractNo"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[4].HeaderText = "合同号";
            dgvPurchaseOrder.Columns[5].Width = 100;
            dgvPurchaseOrder.Columns[5].Name = dtMaster.Columns["ReceivedWH"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[5].HeaderText = "收货仓库";
            dgvPurchaseOrder.Columns[6].Width = 80;
            dgvPurchaseOrder.Columns[6].Name = dtMaster.Columns["Buyer"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[6].HeaderText = "经手人";
            dgvPurchaseOrder.Columns[7].Width = 200;
            dgvPurchaseOrder.Columns[7].Name = dtMaster.Columns["PayAccount"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[7].HeaderText = "付款账户";
            dgvPurchaseOrder.Columns[8].Width = 100;
            dgvPurchaseOrder.Columns[8].Name = dtMaster.Columns["PayAmount"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[8].HeaderText = "付款金额";
            dgvPurchaseOrder.Columns[9].Width = 80;
            dgvPurchaseOrder.Columns[9].Name = dtMaster.Columns["PayDate"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[9].HeaderText = "付款日期";
            dgvPurchaseOrder.Columns[10].Width = 80;
            dgvPurchaseOrder.Columns[10].Name = dtMaster.Columns["InDate"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[10].HeaderText = "进货日期";
            dgvPurchaseOrder.Columns[11].Width = 80;
            dgvPurchaseOrder.Columns[11].Name = dtMaster.Columns["EntryDate"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[11].HeaderText = "录入日期";
            dgvPurchaseOrder.Columns[12].Width = 80;
            dgvPurchaseOrder.Columns[12].Name = dtMaster.Columns["EntryEmployee"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[12].HeaderText = "录入人员";
            dgvPurchaseOrder.Columns[13].Width = 80;
            dgvPurchaseOrder.Columns[13].Name = dtMaster.Columns["InvoiceNo"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[13].HeaderText = "发票编号";
            dgvPurchaseOrder.Columns[14].Width = 80;
            dgvPurchaseOrder.Columns[14].Name = dtMaster.Columns["InvoiceDate"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[14].HeaderText = "发票日期";
            dgvPurchaseOrder.Columns[15].Width = 200;
            dgvPurchaseOrder.Columns[15].Name = dtMaster.Columns["Remark"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[15].HeaderText = "备注";
            dgvPurchaseOrder.Columns[16].Width = 70;
            dgvPurchaseOrder.Columns[16].Name = dtMaster.Columns["RecordID"].ColumnName.ToString();
            dgvPurchaseOrder.Columns[16].HeaderText = "记录ID";
            dgvPurchaseOrder.Columns[16].Visible = false;

            dgvPurchaseOrder.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void FrmPurchaseMain_Load(object sender, EventArgs e)
        {
            dtMaster = purchase.PurchaseMasterTemplate();

            bs = new BindingSource(this.dtMaster, null);
            dgvPurchaseOrder.DataSource = bs;
            SetMasterDataGridView();
        }
    }
}
